import axios from '../../utils/axios'

Component({

  options: {
    styleIsolation: 'apply-shared',
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   */
  properties: {
    flag: Boolean, // 是否外部有调用payModel组件
    commodityList: Object, // 商品list
    showCount: {
      type: Number,
      value: 2
    } // 一行要展示的个数，默认为2
  },

  /**
   * 组件的初始数据
   */
  data: {
    shopp: false,
    commodityInfo: null,
    classifyIndex: 0, // 规格index
  },

  /**
   * 组件的方法列表
   */
  methods: {

    // 点击购买
    shoppBuy: function (e) {
      let goodsId = e.currentTarget.dataset.goodsid

      for (let i = 0; i < this.properties.commodityList.length; i++) {
        if (this.properties.commodityList[i].goods_id == goodsId) {

          // 无规格，则给一个通用
          if (this.properties.commodityList[i].attach == undefined || this.properties.commodityList[i].attach.length <= 0) {
            let param = [{
              attach_id: '-1',
              name: '通用',
              discount: 0,
              price: this.properties.commodityList[i].showMoney,
              showMoney: this.properties.commodityList[i].showMoney,
              showSave: parseFloat(this.properties.commodityInfo[i].showSave).toFixed(2),
              stock: this.properties.commodityList[i].stock
            }]
            this.properties.commodityList[i]['attach'] = param
            if (this.properties.flag) {
              // 向父组件传值
              let paramJson = {
                classifyIndex: 0,
                commodityInfo: this.properties.commodityList[i],
                shopp: true
              }
              this.triggerEvent('shoppModel', paramJson)
            }
            this.setData({
              classifyIndex: 0,
              commodityInfo: this.properties.commodityList[i],
              shopp: true
            })
          } else {
            for (let j = 0; j < this.properties.commodityList[i].attach.length; j++) {
              let itemAttach = this.properties.commodityList[i].attach[j]
              itemAttach['showMoney'] = itemAttach.discount > 0.01 ? itemAttach.discount : itemAttach.price
              itemAttach['showSave'] = parseFloat(itemAttach.price - itemAttach.showMoney).toFixed(2)
            }
            console.log("this.properties.flag",this.properties.flag)
            if (this.properties.flag) {
              // 向父组件传值
              let paramJson = {
                classifyIndex: 0,
                commodityInfo: this.properties.commodityList[i],
                shopp: true
              }
              this.triggerEvent('shoppModel', paramJson)
            }
            this.setData({
              classifyIndex: 0,
              commodityInfo: this.properties.commodityList[i],
              shopp: true
            })
          }
        }
      }

    },

    // 跳转商品详情
    checkDetails: function (e) {
      let goodsId = e.currentTarget.dataset.goodsid
      wx.navigateTo({
        url: '/pages/details/details?goodsId=' + goodsId
      })
    },

  }

})